package com.xiaoh.dormitoryrepairreportbackend.utils;

import com.xiaoh.dormitoryrepairreportbackend.model.dto.workorder.WorkOrderResponse;
import com.xiaoh.dormitoryrepairreportbackend.model.entity.GeneralUser;
import com.xiaoh.dormitoryrepairreportbackend.model.entity.RepairCategory;
import com.xiaoh.dormitoryrepairreportbackend.model.entity.WorkOrder;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 工单转换工具类
 * 用于将工单实体转换为响应DTO，包含脱敏的用户信息
 */
public class WorkOrderConverter {

    /**
     * 将工单实体转换为响应DTO
     * 包含脱敏的用户信息和类别信息
     */
    public static WorkOrderResponse toWorkOrderResponse(WorkOrder workOrder, 
                                                       Map<Long, GeneralUser> userMap,
                                                       Map<Integer, RepairCategory> categoryMap) {
        if (workOrder == null) {
            return null;
        }
        
        WorkOrderResponse response = new WorkOrderResponse();
        BeanUtils.copyProperties(workOrder, response);
        
        // 设置报修人信息（脱敏）
        if (workOrder.getReporterId() != null) {
            GeneralUser reporter = userMap.get(workOrder.getReporterId());
            if (reporter != null) {
                response.setReporterAccount(reporter.getAccountName());
                response.setReporterNickName(reporter.getNickName());
                response.setReporterPhoneNumber(reporter.getPhoneNumber());
                // 不设置敏感信息如密码
            }
        }
        
        // 设置处理人信息（脱敏）
        if (workOrder.getHandlerId() != null) {
            GeneralUser handler = userMap.get(workOrder.getHandlerId());
            if (handler != null) {
                response.setHandlerAccount(handler.getAccountName());
                response.setHandlerNickName(handler.getNickName());
                response.setHandlerPhoneNumber(handler.getPhoneNumber());
            }
        }
        
        // 设置类别信息
        if (workOrder.getCategoryId() != null) {
            RepairCategory category = categoryMap.get(workOrder.getCategoryId());
            if (category != null) {
                response.setCategoryName(category.getCategoryName());
            }
        }
        
        return response;
    }

    /**
     * 批量转换工单实体为响应DTO
     */
    public static List<WorkOrderResponse> toWorkOrderResponseList(List<WorkOrder> workOrders,
                                                                 Map<Long, GeneralUser> userMap,
                                                                 Map<Integer, RepairCategory> categoryMap) {
        if (workOrders == null) {
            return null;
        }
        
        return workOrders.stream()
                .map(workOrder -> toWorkOrderResponse(workOrder, userMap, categoryMap))
                .collect(Collectors.toList());
    }

    /**
     * 从工单列表中提取所有用户ID
     */
    public static List<Long> extractUserIds(List<WorkOrder> workOrders) {
        if (workOrders == null) {
            return List.of();
        }
        
        return workOrders.stream()
                .flatMap(workOrder -> Stream.of(workOrder.getReporterId(), workOrder.getHandlerId()))
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 从工单列表中提取所有类别ID
     */
    public static List<Integer> extractCategoryIds(List<WorkOrder> workOrders) {
        if (workOrders == null) {
            return List.of();
        }
        
        return workOrders.stream()
                .map(WorkOrder::getCategoryId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
    }
}
